<?php
// +----------------------------------------------------------------------
// | BhAdmin [ BhAdmin匠心打造，我们相信，每个伟大的软件都有一个伟大的故事 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020~2023 https://www.bhadmin.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BhAdmin并不是自由软件，未经许可不能去掉BhAdmin相关版权
// +----------------------------------------------------------------------
// | Author: BAIHU  <admin@bhadmin.cn>
// +----------------------------------------------------------------------
namespace app\admin\logic;

use support\Backend\BackendLogic;
use support\Container;
use support\Exception\ApiException;
use support\Service\DictDataService;
use support\Service\DictService;

class DictDataLogic extends BackendLogic
{

    protected $dictService;

    public function __construct()
    {
        $this->service = Container::get(DictDataService::class);
        $this->dictService = Container::get(DictService::class);
    }

    /**
     * 分页列表
     * @return array
     */
    public function getList(): array
    {
        $param = request()->all();
        if (empty($param['dictId'])) return message();
        $where = [];
        $where[] = ["mark", '=', 1];
        $where[] = ["dict_id", '=', (int)$param['dictId']];
        if (!empty($param['name'])) {
            $where[] = ["name", 'like', "%{$param['name']}%"];
        }
        if (!empty($param['code'])) {
            $where[] = ["code", 'like', "%{$param['code']}%"];
        }
        $list = $this->service->getListPage($where, ['*'], ['sort' => 'asc'], [], $param['limit'] ?? 10);
        return message($list);
    }

    public function edit(): array
    {

        $data = request()->post();
        $id = $data['id'] ?? 0;
        if (empty($data['dict_id'])) throw new ApiException('请选择字典分类');
        if (empty($data['name'])) throw new ApiException('请输入字典项标题');
        if (!isset($data['code'])) throw new ApiException('请输入字典项值');
        if (empty($data['sort'])) throw new ApiException('请输入排序');
        $save['dict_id'] = $data['dict_id'];
        $save['name'] = $data['name'];
        $save['sort'] = $data['sort'];
        $save['code'] = $data['code'];
        $save['note'] = $data['note'] ?? '';
        unset($data['id']);
        if ($id == 0) {
            $count = $this->service->getUserCountByWhere([['dict_id', '=', $data['dict_id']], ["code", '=', $save['code']], ["mark", "=", 1]]);
            if ($count > 0) {
                throw new ApiException('系统中已存在相同的配置项值');
            }
            $this->service->add($save);
        } else {
            $count = $this->service->getUserCountByWhere([['dict_id', '=', $data['dict_id']], ["code", '=', $save['code']], ["id", "<>", $id], ["mark", "=", 1]]);
            if ($count > 0) {
                throw new ApiException('系统中已存在相同的配置项值');
            }
            $this->service->save($save, $id);
        }
        return message();

    }

    /**
     * 获取字典项值
     * @return array
     */
    public function getValueList(): array
    {
        $keys = request()->post('keys');
        if (!$keys) return message();
        $dict = $this->dictService->getList([['code', 'in', $keys]]);
        if (!$dict) return message();

        $result = [];
        foreach ($dict as $item) {
            $result[$item['code']] = $this->service->getListByDictId($item['id']);
        }
        return message($result);

    }


}
